package cn.maihe.elg.operation.web.system;

import cn.maihe.elg.operation.model.enums.RoleStatusEnum;
import cn.maihe.elg.operation.model.vo.system.CreateRoleVO;
import cn.maihe.elg.operation.model.vo.system.UpdateRoleVO;
import cn.maihe.elg.operation.repository.entity.RoleDO;
import cn.maihe.elg.operation.repository.entity.entityCustom.RoleDOCustom;
import cn.maihe.elg.operation.security.model.UserContext;
import cn.maihe.elg.operation.service.system.RoleService;
import cn.maihe.elg.operation.utils.UserContextUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JHWei
 */
@Api(value = "角色管理接口", tags = {"角色管理"})
@RestController
@Slf4j
public class RoleController {

    @Resource
    private RoleService roleService;

    @ApiOperation(value = "查询角色")
    @RequestMapping(value = "/api/roles", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "roleName", value = "角色名称", dataType = "String", paramType = "query")
    })
    public PageInfo<RoleDOCustom> findPage(int pageNo, int pageSize, String roleName) {
        log.info("查询全部角色开始");
        return roleService.getRoles(pageNo, pageSize, roleName);
    }

    @ApiOperation(value = "查询角色")
    @RequestMapping(value = "/api/roleSelect", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "userType", value = "用户类型", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "roleName", value = "角色名称", dataType = "String", paramType = "query")
    })
    public PageInfo<RoleDOCustom> roleSelect(int pageNo, int pageSize, int userType, String roleName) {
        log.info("查询全部角色开始");
        UserContext context = UserContextUtil.getUserContext();
        return roleService.getRolesByType(pageNo, pageSize, roleName, userType);
    }

    @ApiOperation(value = "查询角色列表")
    @RequestMapping(value = "/api/roleall", method = RequestMethod.GET)
    public List<RoleDOCustom> findAll() {
        log.info("查询全部角色开始");
        return roleService.getRoles();
    }

    @ApiOperation(value = "查询角色详情")
    @RequestMapping(value = "/api/role/{id}", method = RequestMethod.GET)
    public RoleDO findOne(@PathVariable Long id) {
        log.info("查询角色开始:{}",id);
        return roleService.getRole(id);

    }

    @ApiOperation(value = "创建角色")
    @RequestMapping(value = "/api/role", method = RequestMethod.POST)
    public void createRole(@Validated @RequestBody CreateRoleVO createRoleVO) {
        log.info("保存角色开始");
        RoleDOCustom role = new RoleDOCustom();
        BeanUtils.copyProperties(createRoleVO,role);
//        BeanCopier copier = BeanCopier.create(CreateRoleVO.class, RoleDO.class, false);
//        copier.copy(createRoleVO, role, null);
        role.setStatus(RoleStatusEnum.NORMAL.toString());
        roleService.saveRole(role);
        log.info("保存角色成功");


    }

    @ApiOperation(value = "修改角色")
    @RequestMapping(value = "/api/role", method = RequestMethod.PUT)
    public void updateRole(@Validated @RequestBody UpdateRoleVO updateRoleVO) {
        log.info("修改角色开始！！！");
        RoleDOCustom role = new RoleDOCustom();
        BeanUtils.copyProperties(updateRoleVO,role);
//        BeanCopier copier = BeanCopier.create(UpdateRoleVO.class, RoleDO.class, false);
//        copier.copy(updateRoleVO, role, null);
        roleService.updateRole(role);
        log.info("修改角色结束！！！");

    }

    @ApiOperation(value = "删除角色")
    @RequestMapping(value = "/api/role/{id}", method = RequestMethod.DELETE)
    public void deleteRole(@PathVariable Long id) {
        log.info("删除角色开始！！！");
        roleService.deleteRole(id);
        log.info("删除角色结束！！！");
    }

}
